package com.sdp.refund.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.annotation.OperLog;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.refund.db.refund_batch.entity.RefundBatch;
import com.sdp.refund.db.refund_batch.service.IRefundBatchService;
import com.sdp.right.utils.RightConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-08-31
 */
@RestController
@RequestMapping("/RefundBatchController")
public class RefundBatchController {

	@Autowired
	private Query query;
	
	@Autowired
	private IRefundBatchService iRefundBatchService;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iRefundBatchService, RefundBatch.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	@OperLog(operSystemNo="refund",operDesc="退款批次信息")
	public Result<?> save(@RequestBody RefundBatch batch) {
		if(batch==null) {
			return Result.error("批次信息为空");
		}
		if(StringUtils.isEmpty(batch.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iRefundBatchService, RefundBatch.class, new String[] {"batchName"}, "keyId", batch, null, true);
			if(r==null || !r.isSuccess()) {
				return Result.error("批次名不能重复");
			}
			if(r==null || !r.isSuccess()) {
				return r;
			}
			batch.setCreateTime(LocalDateTime.now());
			batch.setIsComplete(RightConstants.否.getCode());
			iRefundBatchService.save(batch);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iRefundBatchService, RefundBatch.class, new String[] {"batchName"}, "keyId", null, batch, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("批次名不能重复");
			}
			if(r==null || !r.isSuccess()) {
				return r;
			}
			iRefundBatchService.updateById(batch);
		}
		
		return Result.ok();
	}
}

